package mx.mvc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mx.mvc.model.IAction;
import mx.mvc.model.ABean;
import mx.mvc.util.B2AMapping;
import mx.mvc.util.RIBeanUtil;

public class ActionServlet extends HttpServlet {

	private static final long serialVersionUID = -5695207529249558667L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	@SuppressWarnings("rawtypes")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		ABean bean = RIBeanUtil.getBean(request);
		Map<String,String> map = B2AMapping.getMap();
		String actionName = (String)map.get(request.getParameter("className"));
		try {
			Class cls = Class.forName(actionName);
			IAction action=(IAction) cls.newInstance();
			PrintWriter pw= response.getWriter();
			pw.println(action.execute(bean));
			pw.flush();
			pw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
